import 'package:hive/hive.dart';

part 'team_model.g.dart';

@HiveType(typeId: 4)
class TeamModel extends HiveObject {
  @HiveField(0)
  String id;

  @HiveField(1)
  String name;

  @HiveField(2)
  String initiatorId;

  @HiveField(3)
  DateTime departureDate;

  @HiveField(4)
  int maxMembers;

  @HiveField(5)
  List<String> memberIds;

  @HiveField(6)
  String description;

  @HiveField(7)
  String backgroundImage;

  @HiveField(8)
  bool isCollected;

  @HiveField(9)
  String chatId;

  TeamModel({
    required this.id,
    required this.name,
    required this.initiatorId,
    required this.departureDate,
    required this.maxMembers,
    List<String>? memberIds,
    this.description = '',
    required this.backgroundImage,
    this.isCollected = false,
    this.chatId = '',
  }) : this.memberIds = memberIds ?? [initiatorId];

  // 检查团队是否已满
  bool isFull() => memberIds.length >= maxMembers;

  // 添加成员
  bool addMember(String userId) {
    if (isFull() || memberIds.contains(userId)) {
      return false;
    }
    memberIds.add(userId);
    return true;
  }

  // 移除成员
  bool removeMember(String userId) {
    if (userId == initiatorId) return false; // 发起者不能被移除
    return memberIds.remove(userId);
  }
}
